package com.swu.education.controller;

import bitoflife.chatterbean.AliceBot;
import com.swu.education.entity.Progress;
import com.swu.education.service.AliceBotMother;
import com.swu.education.service.DialogService;
import com.swu.education.util.TxtRead;
import com.swu.education.util.TxtWrite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nana on 2017/12/24.
 */

@RestController
public class DialogController {

  @Autowired
  DialogService dialogService;

  AliceBotMother mother = new AliceBotMother();
  TxtWrite txtWrite = new TxtWrite();
  TxtRead txtRead = new TxtRead();

  Map<Integer,AliceBot> botMap = new HashMap();
/*map<id , bot>*/
  @RequestMapping(value = "/jump")
  public String copyMatch(@RequestParam(value="userId") int userId,
                          @RequestParam(value = "activityId") int activityId,
                          @RequestParam(value = "startTime") String startTime) throws Exception {

    mother.setUp();
    String lastResponse="";
    //如果用户没有创建对话，就创建
    if(!botMap.containsKey(userId)){
      AliceBot bot = mother.newInstance(); //创建对话
      lastResponse = bot.respond("begin");  // begin->你好，很高兴见到你
      botMap.put(userId,bot); //存储对话
      dialogService.setDialogStart(userId,activityId,startTime);
      return lastResponse;
    }else {
      AliceBot bot = botMap.get(userId);
      lastResponse = bot.getContext().getResponses(0).toString();
      return lastResponse;
    }
  }

  @RequestMapping(value = "/UserMes")
  public String  getUserMes(@RequestParam(value="userMes") String userMes,
                            @RequestParam(value="userId") int userId,
                            @RequestParam(value = "activityId") int activityId) throws Exception{
    //读取当前用户的id存储的bot
    AliceBot bot = botMap.get(userId);
    System.out.println("user "+userId+" say:"+userMes);
    String responseMes = bot.respond(userMes);
    //botMap.put(userId,bot); //存储对话
    txtWrite.dealTextInfo(activityId,userId,userMes,responseMes);
    return responseMes;
  }

  @RequestMapping(value = "/ReadDialog")
  public String  ReadDialog(@RequestParam(value="userId") int userId,
                            @RequestParam(value = "activityId") int activityId) throws Exception{
    return txtRead.readTxt(activityId,userId);

  }

  @RequestMapping(value = "stopDialogByStuId")
  public String stopDialogByStuId(@RequestParam(value = "userId") int userId){
    if(botMap.containsKey(userId)){
      botMap.remove(userId);
      return "success";
    }
    if (!botMap.containsKey(userId)) {
      return "fail";
    }
    return "fail";
  }
  @RequestMapping(value = "stopAllDialogByCourseId")
  public void stopAllDialogById(@RequestParam("courseId") int courseId ){
    List userIdList = dialogService.removeAllStuDialogByCourseId(courseId);
    for(int i=0;i<userIdList.size();i++){
      if(botMap.containsKey(userIdList.get(i))){
        botMap.remove(userIdList.get(i));
      }
    }
  }

  //删除学生对话信息
  @RequestMapping(value = "deleteDialogFile")
  public void deleteDialogFile(@RequestParam("activityId") int activityId,
                               @RequestParam("userId") int userId){
    txtRead.deleteTxt(activityId,userId);
  }

  @RequestMapping(value = "getProgressIndex")
  public int getProgressIndex(@RequestParam("userId") int userId,
                               @RequestParam("activityId") int activityId){
    int progressIndex = dialogService.getStudentProgress(userId,activityId);
    return progressIndex;
  }

  @RequestMapping(value = "setProgressIndex")
  public void setProgressIndex(@RequestParam("userId") int userId,
                              @RequestParam("activityId") int activityId,
                              @RequestParam("progressIndex") int progressIndex){
    dialogService.setStudentProgress(userId,activityId,progressIndex);
  }

/*  @RequestMapping(value = "setDialogStartTime")
  public void getDialogStartTime(@RequestParam("userId") int userId,
                               @RequestParam("activityId") int activityId,
                               @RequestParam("startTime") int startTime){
    dialogService.setDialogStart(userId,activityId,startTime);
  }*/

  @RequestMapping(value = "setDialogEndTime")
  public void setDialogEndTime(@RequestParam("userId") int userId,
                                 @RequestParam("activityId") int activityId,
                                 @RequestParam("endTime") String endTime){
    System.out.println("endTime: "+endTime);
    dialogService.setDialogEnd(userId,activityId,endTime);
  }

}